package com.ruoyi.home.controller;

import java.util.List;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.home.domain.SuBannerAds;
import com.ruoyi.home.domain.SuExternalGoods;
import com.ruoyi.home.service.ISuBannerAdsService;
import com.ruoyi.home.service.ISuExternalGoodsService;
import com.ruoyi.campus.domain.Campus;
import com.ruoyi.campus.domain.vo.CampusVO;
import com.ruoyi.campus.service.ICampusService;

/**
 * 首页管理Controller
 * 
 * @author ruoyi
 * @date 2025-06-20
 */
@RestController
@RequestMapping("/home/management")
public class HomeManagementController extends BaseController
{
    @Autowired
    private ISuBannerAdsService bannerAdsService;
    
    @Autowired
    private ISuExternalGoodsService externalGoodsService;
    
    @Autowired
    private ICampusService campusService;

    /**
     * 获取首页数据（支持按校区筛选）
     */
    @GetMapping("/data")
    public AjaxResult getHomeData(@RequestParam(value = "campusId", required = false) Integer campusId)
    {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 查询轮播图数据
            SuBannerAds bannerQuery = new SuBannerAds();
            bannerQuery.setStatus(1); // 只查询已上架的
            if (campusId != null) {
                bannerQuery.setCampusId(campusId);
            }
            List<SuBannerAds> bannerList = bannerAdsService.selectSuBannerAdsList(bannerQuery);
            
            // 查询商品数据
            SuExternalGoods goodsQuery = new SuExternalGoods();
            goodsQuery.setStatus(1); // 只查询已上架的
            if (campusId != null) {
                goodsQuery.setCampusId(campusId);
            }
            List<SuExternalGoods> goodsList = externalGoodsService.selectSuExternalGoodsList(goodsQuery);
            
            // 查询校区信息（用于显示）
            Campus currentCampus = null;
            if (campusId != null) {
                currentCampus = campusService.selectCampusByCampusId(campusId);
            }
            
            result.put("bannerList", bannerList);
            result.put("goodsList", goodsList);
            result.put("currentCampus", currentCampus);
            result.put("campusId", campusId);
            result.put("isAdmin", campusId == null); // 如果没有campusId，说明是超级管理员
            
            return AjaxResult.success("获取首页数据成功", result);
            
        } catch (Exception e) {
            logger.error("获取首页数据失败", e);
            return AjaxResult.error("获取首页数据失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取所有校区列表（超级管理员使用）
     */
    @GetMapping("/campus/list")
    public AjaxResult getCampusList()
    {
        try {
            CampusVO campusVO = new CampusVO();
            campusVO.setCampusStatus(1); // 只查询状态正常的校区
            List<CampusVO> campusList = campusService.selectCampusList(campusVO);
            return AjaxResult.success("获取校区列表成功", campusList);
        } catch (Exception e) {
            logger.error("获取校区列表失败", e);
            return AjaxResult.error("获取校区列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据校区查询轮播图
     */
    @GetMapping("/banners")
    public AjaxResult getBannersByCampus(@RequestParam(value = "campusId", required = false) Integer campusId)
    {
        try {
            SuBannerAds bannerQuery = new SuBannerAds();
            bannerQuery.setStatus(1); // 只查询已上架的
            if (campusId != null) {
                bannerQuery.setCampusId(campusId);
            }
            
            List<SuBannerAds> bannerList = bannerAdsService.selectSuBannerAdsList(bannerQuery);
            return AjaxResult.success("获取轮播图成功", bannerList);
            
        } catch (Exception e) {
            logger.error("获取轮播图失败", e);
            return AjaxResult.error("获取轮播图失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据校区查询商品
     */
    @GetMapping("/goods")
    public AjaxResult getGoodsByCampus(@RequestParam(value = "campusId", required = false) Integer campusId)
    {
        try {
            SuExternalGoods goodsQuery = new SuExternalGoods();
            goodsQuery.setStatus(1); // 只查询已上架的
            if (campusId != null) {
                goodsQuery.setCampusId(campusId);
            }
            
            List<SuExternalGoods> goodsList = externalGoodsService.selectSuExternalGoodsList(goodsQuery);
            return AjaxResult.success("获取商品成功", goodsList);
            
        } catch (Exception e) {
            logger.error("获取商品失败", e);
            return AjaxResult.error("获取商品失败：" + e.getMessage());
        }
    }
} 